package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

public class TreeFrame extends JFrame {
	JTree tree;
	JScrollPane jsp;
	JButton show, hide;

	TreeFrame(DefaultMutableTreeNode x) {
		this.setSize(600, 400);
		this.setLocationRelativeTo(null);

		tree = new JTree(x);
		jsp = new JScrollPane(tree);
		this.add(jsp, BorderLayout.CENTER);
		this.setVisible(true);

		DefaultTreeCellRenderer r = new DefaultTreeCellRenderer();
		r.setLeafIcon(new ImageIcon("icon/Terminal.png"));
		r.setClosedIcon(new ImageIcon("icon/nonTerminal.png"));
		r.setOpenIcon(new ImageIcon("icon/nonTerminalOpened.png"));
		tree.setCellRenderer(r);
		tree.getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);

		JToolBar jtb = new JToolBar("Microscript-Syntax Analysis");
		jtb.add(show = new JButton("Show All From Here(Alt+S)"));
		jtb.addSeparator();
		jtb.add(hide = new JButton("Hide All From Here(Alt+H)"));
		show.setFocusPainted(false);
		show.setBorderPainted(false);
		hide.setFocusPainted(false);
		hide.setBorderPainted(false);
		this.add(jtb, BorderLayout.NORTH);// Add Toolbar

		show.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DefaultMutableTreeNode selected = (DefaultMutableTreeNode) tree
						.getLastSelectedPathComponent();
				if(selected==null)
					JOptionPane.showMessageDialog(null,"Warning: No node selected!","Error",JOptionPane.WARNING_MESSAGE);
				else
					expandTreeNode(tree, selected);
			}
		});
		hide.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DefaultMutableTreeNode selected = (DefaultMutableTreeNode) tree
						.getLastSelectedPathComponent();
				if(selected==null)
					JOptionPane.showMessageDialog(null,"Warning: No node selected!","Error",JOptionPane.WARNING_MESSAGE);
				else
					collapseTreeNode(tree, selected);
			}
		});
		
		show.setMnemonic('S');
		hide.setMnemonic('H');
	}

	public void expandTreeNode(JTree aTree, DefaultMutableTreeNode aNode) {
		if (aNode.isLeaf()) {
			return;
		}
		aTree.expandPath(new TreePath(((DefaultMutableTreeNode) aNode)
				.getPath()));
		int n = aNode.getChildCount();
		for (int i = 0; i < n; i++) {
			expandTreeNode(aTree, (DefaultMutableTreeNode) aNode.getChildAt(i));
		}
	}

	public void collapseTreeNode(JTree aTree, DefaultMutableTreeNode aNode) {
		if (aNode.isLeaf()) {
			return;
		}
		int n = aNode.getChildCount();
		for (int i = 0; i < n; i++) {
			collapseTreeNode(aTree, (DefaultMutableTreeNode) aNode.getChildAt(i));
		}
		aTree.collapsePath(new TreePath(((DefaultMutableTreeNode) aNode)
				.getPath()));
	}

}